public class test2 {
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (slow!= fast) {
            // 如果fast走到了空 就直接返回false
            if (fast == null || fast.next == null) {
                return false;
            }
            // slow每次都一步  fast每次走两步
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}
